﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SIOsystem.Control;

namespace SIOsystem
{
    public partial class ApplyForm : Form
    {
        public string uname = "";
        public long id = -1;
        public string rid = "";
        private BindingList<OrderListClass> orderListDateSet = new BindingList<OrderListClass>();
        public ApplyForm()
        {
            InitializeComponent();
        }

        private void ApplyForm_Load(object sender, EventArgs e)
        {
            DictionaryClass.fillDepartment(ref DepartmentList);
            DictionaryClass.fillMainClass(ref MainClassList);
            orderListDateSet.Clear();
            ReBinding();
        }
        private void MainClassList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (MainClassList.Items.Count > 0)
            {
                long MID = 0;
                if (MainClassList.SelectedValue != null)
                {
                    long.TryParse(MainClassList.SelectedValue.ToString(), out MID);
                }
                if (MID > 0)
                {
                    DictionaryClass.fillProductClass(ref ProductList, MID);
                }
                else
                {
                    ProductList.DataSource = null;
                    ProductList.Items.Clear();
                }
            }
        }

        private void ProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProductList.Items.Count > 0)
            {
                long PID = 0;
                if (ProductList.SelectedValue != null)
                {
                    long.TryParse(ProductList.SelectedValue.ToString(), out PID);
                }
                if (PID > 0)
                {
                    DictionaryClass.fillModelClass(ref ModelList, PID);
                    ProductLabel.Text = ProductList.Text;
                }
                else
                {
                    ProductLabel.Text = "";
                    ModelList.DataSource = null;
                    ModelList.Items.Clear();
                }
            }
        }

        private void ModelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ModelList.Items.Count > 0)
            {
                long TID = 0;
                if (ModelList.SelectedValue != null)
                {
                    long.TryParse(ModelList.SelectedValue.ToString(), out TID);
                }
                if (TID > 0)
                {
                    JoinOrder.Enabled = true;
                    double[] storageAndPrice = OrderClass.getStorage(TID);
                    StorageLabel.Text = storageAndPrice[0].ToString("#0");
                    //NewPrice.Value = decimal.Parse(storageAndPrice[1].ToString("#0.00#"));
                }
                else
                {
                    StorageLabel.Text = "0";
                    //NewPrice.Value = 0;
                    JoinOrder.Enabled = false;
                }
            }
        }

        private void JoinOrder_Click(object sender, EventArgs e)
        {
            long MID = 0, PID = 0, TID = 0;
            if ( InAmtPick.Value == 0)
            {
                MessageBox.Show("申请数量不能为空");
                return;
            }
            if (MainClassList.SelectedValue != null)
            {
                long.TryParse(MainClassList.SelectedValue.ToString(), out MID);
            }
            if (ProductList.SelectedValue != null)
            {
                long.TryParse(ProductList.SelectedValue.ToString(), out PID);
            }
            if (ModelList.SelectedValue != null)
            {
                long.TryParse(ModelList.SelectedValue.ToString(), out TID);
            }
            if (MID > 0 && PID > 0 && TID > 0)
            {
                orderListDateSet.Add(new OrderListClass(MID, PID, TID, ProductLabel.Text + " " + ModelList.Text
                    , Convert.ToInt32(InAmtPick.Value),0, remark.Text));
                ReBinding();
                InAmtPick.Value = 0;
                ModelList.SelectedIndex = 0;
                remark.Text = "";
                ModelList.Focus();
            }
        }

        private void InAmtPick_Enter(object sender, EventArgs e)
        {
            InAmtPick.Select(0, InAmtPick.Value.ToString().Length);
        }

        private void OrderList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            orderListDateSet.RemoveAt(e.RowIndex);
            ReBinding();
        }

        private void ReBinding()
        {
            double sum = 0;
            foreach (OrderListClass ol in orderListDateSet)
            {
                sum += ol.Amt * ol.Price;
            }
            //MoneySum.Text = sum.ToString("#0.00#");
            OrderList.DataSource = new BindingList<OrderListClass>(orderListDateSet);
        }

        private void InsertOrder_Click(object sender, EventArgs e)
        {
            if (orderListDateSet.Count == 0)
            {
                MessageBox.Show("申购单为空！申购前先将商品信息添加进申购单");
                return;
            }
            long IOID = -1;
            if (OrderClass.insertOrder(orderListDateSet, "", id,long.Parse(DepartmentList.SelectedValue.ToString()), ref IOID))
            {
                MessageBox.Show("申购成功！");
                orderListDateSet.Clear();
                ReBinding();
                //InputName.Text = "";
                MainClassList.SelectedIndex = 0;
                ReportClass.PrintInOrder(IOID);
            }
            else
            {
                MessageBox.Show("商品信息有误，申购失败，请检查申购单中数据格式");
            }
        }
    }
}
